local BaseWnd = require(SCRIPTS_DIR .. ".wnds.BaseWnd");
local LoginWnd = class("LoginWnd",BaseWnd);
local gameState = require("framework.cc.utils.GameState");
function LoginWnd:ctor()
	LoginWnd.super.ctor(self);
end

function LoginWnd:onOpen()
	local wndName = self.wndData.name;
    self:setPhotoClick();
    self:setDuiHuan1Click();
    self:setJiuJiJinClick();
    self:setPaiHangClick();
    self:setDuiHuan2Click();
    self:setChengJiuClick();
    self:setSheZhiClick();
    self:setChongZhiClick();
    self:setYueKaClick();
    self:setTeQuanClick();
    self:setShangChengClick();
    self:setXiuXianClick();
    self:setJinJiClick();
    --self:connectSocket();
    self:addEventListener(enums.Event.WebSocketOpen,handler(self,self.login));
    self:addEventListener(enums.Event.ShowRoleBaseInfo,handler(self,self.showRoleBaseInfo))
    self:addEventListener(enums.Event.WebSocketClose,handler(self,self.webSocketClose));
end

function LoginWnd:webSocketClose()
    local photo = self:widget("left.photoPanel.phBg");
    photo:setVisible(false);
end

function LoginWnd:showRoleBaseInfo()
    self:widget("middle.lv"):setString("Lv:" .. mgrs.data:getLoginData():getLevel());
    self:widget("middle.gem"):setString("Gem:" .. mgrs.data:getLoginData():getGem());
    self:widget("middle.coin"):setString("Coin:" .. mgrs.data:getLoginData():getCoin());
end

function LoginWnd:setPhotoClick()
    local photo = self:widget("left.photoPanel.phBg");
    self:createClick({targetNode = photo}):clickHandler(function()
        mgrs.log:printDebug("photo");
    end);
end


function LoginWnd:setDuiHuan1Click()
    local duihuan1 = self:widget("left.duihuan1");
    self:createClick({targetNode = duihuan1}):clickHandler(function()
        mgrs.log:printDebug("duihuan1");
    end);
end

function LoginWnd:setJiuJiJinClick()
    local jiujijin = self:widget("left.jiujijin");
    self:createClick({targetNode = jiujijin}):clickHandler(function()
        mgrs.log:printDebug("jiujijin");
    end);
end

function LoginWnd:setPaiHangClick()
    local paihang = self:widget("left.paihang");
    self:createClick({targetNode = paihang}):clickHandler(function()
        mgrs.log:printDebug("paihang");
    end);
end

function LoginWnd:setDuiHuan2Click()
    local duihuan2 = self:widget("left.duihuan2");
    self:createClick({targetNode = duihuan2}):clickHandler(function()
        mgrs.log:printDebug("duihuan2");
    end);
end

function LoginWnd:setChengJiuClick()
    local chengjiu = self:widget("left.chengjiu");
    self:createClick({targetNode = chengjiu}):clickHandler(function()
        mgrs.log:printDebug("chengjiu");
    end);
end

function LoginWnd:setSheZhiClick()
    local shezhi = self:widget("left.shezhi");
    self:createClick({targetNode = shezhi}):clickHandler(function()
        mgrs.log:printDebug("shezhi");
    end);
end

function LoginWnd:setChongZhiClick()
    local chongzhi = self:widget("right.chongzhi");
    self:createClick({targetNode = chongzhi}):clickHandler(function()
        mgrs.log:printDebug("chongzhi");
    end);
end

function LoginWnd:setYueKaClick()
    local yueka = self:widget("right.yueka");
    self:createClick({targetNode = yueka}):clickHandler(function()
        mgrs.log:printDebug("yueka");
    end);
end

function LoginWnd:setTeQuanClick()
    local tequan = self:widget("right.tequan");
    self:createClick({targetNode = tequan}):clickHandler(function()
        mgrs.log:printDebug("tequan");
    end);
end

function LoginWnd:setShangChengClick()
    local shangcheng = self:widget("right.shangcheng");
    self:createClick({targetNode = shangcheng}):clickHandler(function()
        mgrs.log:printDebug("shangcheng");
    end);
end

function LoginWnd:setXiuXianClick()
    local xiuxian = self:widget("middle.xiuxian");
    self:createClick({targetNode = xiuxian}):clickHandler(function()
        mgrs.wnd:openWnd(enums.Wnd.Chose,{battleKind = 1});
    end);
end

function LoginWnd:setJinJiClick()
    local jinji = self:widget("middle.jinji");
    self:createClick({targetNode = jinji}):clickHandler(function()
        mgrs.wnd:openWnd(enums.Wnd.Chose,{battleKind = 2});
    end);
end

function LoginWnd:connectSocket()
    mgrs.overTime:show(enums.OverTime.Login);
    local ip = HttpData.endData.ip;
    local port = HttpData.endData.port;
    if ip and port then
        -- body
        mgrs.log:printDebug("LoginWnd:connectSocket");
        local address = string.format("ws://%s:%s",ip,port);
        mgrs.proto:connect(address,nil);
    else
        mgrs.hint:showHint("创建socket失败");
        mgrs.behave:nextBehave(enums.Behave.Hint);
    end
end

function LoginWnd:login()
    mgrs.overTime:hide(enums.OverTime.Login);
    local d = {msgid = Protocol.C2A_ROLE_AUTHENTICATE,passport = HttpData.endData.ps};
    mgrs.proto:send(json.encode(d));
end


function LoginWnd:reqData(handler)
	handler(true);
end

--[[
function LoginWnd:getRelativePosition()
	return {rx = 0.5; ry = 0.5; ox = mgrs.wnd.visibleRect.center.x; oy = mgrs.wnd.visibleRect.center.y;	};
end
--]]

function LoginWnd:getLayer()
	return enums.Layer.Main;
end

function LoginWnd:getPreloadResList()
	return {
        Png = {"denglujiemian"}
    };
end

function LoginWnd:getExclusiveType()
	return 0;
end


function LoginWnd:isOpenCloseSoundEnabled()
	return false;
end	


function LoginWnd:onClose()
    mgrs.log:printFuncCall();
end

function LoginWnd:onEnter()
	mgrs.log:printFuncCall();
end
function LoginWnd:onExit()
	mgrs.log:printFuncCall();
end
function LoginWnd:onEnterTransitionFinish()
	mgrs.log:printFuncCall();
end

function LoginWnd:onExitTransitionStart()
	mgrs.log:printFuncCall();
end

function LoginWnd:onCleanup()
	mgrs.log:printFuncCall();
    LoginWnd.super.onCleanup(self);
end

mgrs.wnd:registerWnd(enums.Wnd.Login,LoginWnd);